﻿/*
 * Copyright(C) 2012,ajayumi 保留所有权利。( All rights reserved. )
 * 
 * 文件名称：AjaxData.cs
 * 摘    要：
 * 当前版本：1.0
 * 作    者：黄乙冬 (ajayumi)
 * 创建日期：2012年6月1日星期五
 */
using System.Collections.Generic;
using System.Text;
using System.Web;
using System;
using System.Reflection;

namespace ajayumi.Platform.Web.Controls.DataGrid
{
    public class JQueryGridAjaxData : IHttpHandler
    {

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string _operator = context.Request.QueryString["operator"];
            string action = context.Request.QueryString["action"];
            // get the context.Requested page
            int currentPage = ConvertHelper.ToInt(context.Request.QueryString["page"]);
            // get how many rows we want to have into the grid
            int pageSize = ConvertHelper.ToInt(context.Request.QueryString["rows"]);
            // get index row - i.e. user click to sort
            string sidx = context.Request.QueryString["sidx"];
            // get the direction
            string sord = context.Request.QueryString["sord"];

            Operation op = Operation.None;
            if (!string.IsNullOrEmpty(action))
            {
                op = (Operation)Enum.Parse(typeof(Operation), CryptoString.DecryptText(action), true);
            }

            string result = string.Empty;
            switch (op)
            {
                case Operation.Create:
                    break;
                case Operation.Read:
                    result = this.GetList(currentPage, pageSize);
                    //result = this.m_Operator.GetList(currentPage, pageSize);
                    break;
                case Operation.Update:
                    break;
                case Operation.Delete:
                    break;
            }
            context.Response.Write(result);

        }

        public virtual string GetList(int currentPage, int pageSize)
        {
            int recordCount = 100;
            int totalPages = 0;
            if (recordCount % pageSize == 0)
            {
                totalPages = recordCount / pageSize;
            }
            else
            {
                totalPages = recordCount / pageSize + 1;
            }

            PersonCollection persons = new PersonCollection();

            StringBuilder sb = new StringBuilder();
            sb.Append("{");
            sb.Append("\"page\":" + currentPage + ",");
            sb.Append("\"total\":" + totalPages + ",");
            sb.Append("\"records\":" + recordCount + ",");
            sb.Append("\"rows\":[");

            List<PersonInfo> lst = persons.GetList(currentPage, pageSize);
            foreach (PersonInfo item in lst)
            {
                sb.AppendFormat("{{\"id\":\"{0}\",", item.Id);
                sb.Append("\"cell\":[");
                sb.AppendFormat("\"{0}\", \"{1}\",\"{2}\"", item.Id, item.Code, item.Name);
                sb.Append("]");
                sb.Append("},");
            }

            sb.Remove(sb.Length - 1, 1);
            sb.Append("]");
            sb.Append("}");

            return sb.ToString();
        }


    }
}
